"""
@Author: li
@Email: lijianqiao2906@live.com
@FileName: permission.py
@DateTime: 2025/07/06
@Docs: 权限相关的 Pydantic Schema
"""

from pydantic import Field

from app.schemas.base import BaseRequest


class PermissionBase(BaseRequest):
    """权限基础 Schema"""

    name: str = Field(max_length=100, description="权限名称")
    code: str = Field(max_length=100, description="权限编码,格式: system:user:create")
    type: str = Field(max_length=20, description="权限类型 (eg: module, menu, button)")
    resource: str | None = Field(default=None, max_length=100, description="资源标识")
    action: str | None = Field(default=None, max_length=50, description="操作类型 (eg: create, delete, update, list)")
    parent_id: int | None = Field(default=None, description="父权限ID")
    sort_order: int = Field(default=0, description="排序")
    description: str | None = Field(default=None, description="描述")


class PermissionCreate(PermissionBase):
    """创建权限 Schema"""

    pass


class PermissionUpdate(BaseRequest):
    """更新权限 Schema (所有字段可选)"""

    name: str | None = Field(default=None, max_length=100, description="权限名称")
    code: str | None = Field(default=None, max_length=100, description="权限编码,格式: system:user:create")
    type: str | None = Field(default=None, max_length=20, description="权限类型 (eg: module, menu, button)")
    resource: str | None = Field(default=None, max_length=100, description="资源标识")
    action: str | None = Field(default=None, max_length=50, description="操作类型 (eg: create, delete, update, list)")
    parent_id: int | None = Field(default=None, description="父权限ID")
    sort_order: int | None = Field(default=None, description="排序")
    description: str | None = Field(default=None, description="描述")


class PermissionRead(PermissionBase):
    """读取权限 Schema"""

    id: int = Field(description="权限ID")

    class Config:
        from_attributes = True


class PermissionNode(PermissionRead):
    """权限树节点 Schema"""

    children: list["PermissionNode"] = Field(default_factory=list, description="子权限")
